package com.yunxin.admin.system.api.system;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunxin.admin.system.application.RoleQueryService;
import com.yunxin.admin.system.domain.model.user.UserMerchantRepository;
import com.yunxin.admin.system.infra.common.constant.SecurityConstants;
import com.yunxin.admin.system.infra.framework.resubmit.Resubmit;
import com.yunxin.admin.system.infra.framework.security.util.SecurityUtils;
import com.yunxin.admin.system.infra.persistence.entity.SysUserRoleDO;
import com.yunxin.common.result.PageResult;
import com.yunxin.common.result.Result;
import com.yunxin.core.application.command.page.WeightPageCommand;
import com.yunxin.core.application.service.SysUserWeightService;
import com.yunxin.core.domain.model.weight.SysUserWeight;
import com.yunxin.core.domain.model.weight.SysUserWeightDto;
import com.yunxin.core.domain.model.weight.SysWeightDelete;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户权重接口")
@RestController
@RequestMapping("/api/v1/weight/user")
@RequiredArgsConstructor
public class SysUserWeightController {


    private final RoleQueryService roleQueryService;

    private final SysUserWeightService sysUserWeightService;

    private final UserMerchantRepository userMerchantRepository;

    @Operation(summary = "权重分页列表-用户", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PostMapping("/page")
    public PageResult<SysUserWeight> getUserWeightPage(@RequestBody WeightPageCommand command) {
        // 每个商户只能看到自己的团队权重
        Long userId = SecurityUtils.X.getUser().getUserId();
        List<Long> listByUserId = roleQueryService.getRoleListByUserId(userId).stream().map(SysUserRoleDO::getRoleId).toList();
        if(!listByUserId.isEmpty()) if (listByUserId.contains(1L)) {
            return PageResult.success(null, 0);
        }
        Long merchantId = userMerchantRepository.selectMerchantIdByUserId(userId);
        command.setMerchantId(merchantId);

        IPage<SysUserWeight> result = sysUserWeightService.getUserWeightPage(command);
        return PageResult.success(result.getRecords(), result.getTotal());
    }


    @Operation(summary = "新增权重-用户", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PostMapping("/add")
    @PreAuthorize("@ss.hasPerm('weight:personal:add')")
    @Resubmit
    public Result<Boolean> add(@Validated  @RequestBody SysUserWeightDto command) {
        Long userId = SecurityUtils.X.getUser().getUserId();
        Long merchantId = userMerchantRepository.selectMerchantIdByUserId(userId);
        command.setMerchantId(merchantId);
        return Result.judge(sysUserWeightService.saveUserWeigh(command));
    }


    @Operation(summary = "修改权重-用户", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PostMapping("/edit")
    @PreAuthorize("@ss.hasPerm('weight:personal:edit')")
    @Resubmit
    public Result<Boolean> edit(@Validated  @RequestBody SysUserWeightDto command) {
        Long userId = SecurityUtils.X.getUser().getUserId();
        Long merchantId = userMerchantRepository.selectMerchantIdByUserId(userId);
        command.setMerchantId(merchantId);
        return Result.judge(sysUserWeightService.editUserWeigh(command));
    }




    @Operation(summary = "删除权重", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PostMapping("/delete")
    @PreAuthorize("@ss.hasPerm('weight:personal:delete')")
    @Resubmit
    public Result<Boolean> delete(@Validated  @RequestBody SysWeightDelete command) {
        return Result.judge(sysUserWeightService.deleteUserWeight(command));
    }

    @Operation(summary = "权重开关", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/weightSwitch")
    @PreAuthorize("@ss.hasPerm('weight:personal:edit')")
    @Resubmit
    public Result<Boolean> weightSwitch(@RequestParam("id") Integer id,
                                        @RequestParam("status") Integer status) {
        SysUserWeight weight = new SysUserWeight();
        weight.setId(id);
        weight.setStatus(status);
        return Result.judge(sysUserWeightService.weightSwitch(weight));
    }

    @Operation(summary = "用户权重下拉列表", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/list")
    public Result<List<SysUserWeight>> getUserWeightList() {
        Long userId = SecurityUtils.X.getUser().getUserId();
        Long merchantId = userMerchantRepository.selectMerchantIdByUserId(userId);

        return Result.success(sysUserWeightService.getUserWeightList(merchantId));
    }



}
